package dao

import (
	"go-zero-demo/user-rpc/internal/dao/entity"
	"gorm.io/gorm"
)

func GetUserInfo(db *gorm.DB, userId int32) (entity.UserEntity, error) {
	tx := db.Session(&gorm.Session{SkipDefaultTransaction: true})
	var userEntity entity.UserEntity
	selectSql := "user.`id`, user.`mobile`, user.`password`, user.`nick_name`, user.`head_url`," +
		" user.`birthday`, user.`address`," +
		" user.`desc`, user.`gender`,  user.`status`, user.`create_time`, user.`update_time`"
	tx.Table("user").Select(selectSql).
		Where("user.id=?", userId).First(&userEntity)
	return userEntity, tx.Error
}

func CreateUserTx(tx *gorm.DB, data *entity.UserEntity) (int32, error) {
	createRs := tx.Debug().Create(data)
	if createRs.Error != nil {
		// rollback ...
		tx.Rollback()
		return -1, createRs.Error
	}
	return int32(data.ID), nil
}
